import entity.GroupMessage;
import enums.ResponseType;
import request.group.message.GroupMessageSendRequest;
import request.group.relation.GroupRelationBuildRequest;
import request.group.relation.GroupRelationDestroyRequest;
import response.ErrorResponse;
import response.Response;
import response.group.message.GroupMessageSendResponse;
import response.group.relation.GroupRelationBuildResponse;
import response.group.relation.GroupRelationDestroyResponse;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.time.LocalDateTime;

public class GroupChatHandler extends KeyAdapter implements ActionListener {
    private GroupChatView groupChatView;

    public GroupChatHandler(GroupChatView groupChatView) {
        this.groupChatView = groupChatView;
    }

    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("发送".equals(text)) {
            send();
        }
        else if("添加成员".equals(text)){
            addMember();
        }
        else if("退出群聊".equals(text)){
            quitGroup();
        }
    }

    public void send() {
        String content = groupChatView.getTextField().getText();
        GroupMessage message = new GroupMessage(groupChatView.getUUID(),groupChatView.getHostUserName(),LocalDateTime.now(),content);
        GroupMessageSendRequest messageSendRequest = new GroupMessageSendRequest(message);
        Response response = ConnectionUtil.makeConnection(messageSendRequest);
        if (response.getType() == ResponseType.GroupMessageSend) {
            GroupMessageSendResponse messageSendResponse = (GroupMessageSendResponse) response;
            groupChatView.updateList(message);
        }
        else if(response.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(groupChatView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void addMember(){
        String friendUserName=groupChatView.getTextField().getText();
        GroupRelationBuildRequest groupRelationBuildRequest=new GroupRelationBuildRequest(friendUserName,groupChatView.getUUID());
        Response response=ConnectionUtil.makeConnection(groupRelationBuildRequest);
        if(response.getType()==ResponseType.GroupRelationBuild){
            GroupRelationBuildResponse groupRelationBuildResponse=(GroupRelationBuildResponse) response;
            JOptionPane.showMessageDialog(groupChatView, "已添加该好友至群！");
        }
        else if(response.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(groupChatView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void quitGroup(){
        String hostUserName=groupChatView.getHostUserName();
        GroupRelationDestroyRequest groupRelationDestroyRequest=new GroupRelationDestroyRequest(hostUserName,groupChatView.getUUID());
        Response response=ConnectionUtil.makeConnection(groupRelationDestroyRequest);
        if(response.getType()==ResponseType.GroupRelationDestroy){
            GroupRelationDestroyResponse groupRelationDestroyResponse=(GroupRelationDestroyResponse) response;
            JOptionPane.showMessageDialog(groupChatView, "已退出该群！");
            groupChatView.dispose();
        }
        else if(response.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(groupChatView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void keyPressed(KeyEvent e) {
        if (KeyEvent.VK_ENTER == e.getKeyCode()) {
            send();
        }
    }
}
